
#include "SDL.h"
#include "logger.hpp"
#include <string>

SDL_Surface* load_image(std::string filename)
{
	//Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

	//Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );

	//If nothing went wrong in loading the image
	if(loadedImage != NULL)
	{
		//Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old image
        SDL_FreeSurface( loadedImage );
	}

	return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;

	//Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}
/*
bool init(SDL_Surface *screen,int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP)
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;    
    }
    
    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;    
    }
    
    //Set the window caption
    SDL_WM_SetCaption( "Event test", NULL );
    
    //If everything initialized fine
    return true;
}
*/

void refresh(int x,int y, SDL_Rect *rect,int keyFlag, SDL_Surface *source,SDL_Surface *bg , SDL_Surface *destination)
{
	//El fondo siempre en el origen.
	apply_surface( 0, 0, bg, destination );

	switch(keyFlag){
		//IZQ
		case 1: if((rect->w + x) >= 800)
				{	
					apply_surface( x, y, source, destination );
					SDL_Flip( destination );
				}				
				break;
		//DER
		case 2: if(x < 0)
				{	
					apply_surface( x, y, source, destination );
					SDL_Flip( destination );
				}				

				break;
		//UP
		case 3: if((rect->h + y) >= 600)
				{	
					apply_surface( x, y, source, destination );
					SDL_Flip( destination );
				}				
				break;
		//DOWN
		case 4: if(y < 0)
				{	
					apply_surface( x, y, source, destination );
					SDL_Flip( destination );
				}				
				break;
	}
	
}

int main( int argc, char* args[] )
{
	const int SCREEN_WIDTH	= 800;
	const int SCREEN_HEIGHT = 600;
	const int SCREEN_BPP	= 32;

	SDL_Surface *background = NULL;
	SDL_Surface *screen		= NULL;
	SDL_Surface *map		= NULL;
	SDL_Surface *izq		= NULL;
	SDL_Surface *der		= NULL;
	SDL_Surface *top		= NULL;
	SDL_Surface *bot		= NULL;


	    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;    
    }
    
    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;    
    }
    
    //Set the window caption
    SDL_WM_SetCaption( "Event test", NULL );
    
 	//Load the images
	background	= load_image( "fondo3.bmp" );
	map			= load_image( "pic1440-900.bmp" );
	//izq			= load_image( "KeyPressedbaseIzq.bmp" );
	//der			= load_image( "KeyPressedbaseDer.bmp" );
	//top			= load_image( "KeyPressedbaseTop.bmp" );
	//bot			= load_image( "KeyPressedbaseBot.bmp" );

	//Apply the background to the screen
    apply_surface( 0, 0, background, screen );
		
	//Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;    
    }

	int despX = 0;
	int despY = 0;

	//The event structure that will be used
	SDL_Event eventSdl;
	SDL_Rect  rect;
	Logger	  logger; 
	logger.open("log.txt");
	bool	  quit = false;
	std::string detail;

	rect.x = 0;
	rect.y = 0;
	rect.w = 1440;
	rect.h = 900;

	  //While the user hasn't quit
    while( quit == false )
    {
		//While there's an event to handle
		while( SDL_PollEvent( &eventSdl ) )
		{
			if( eventSdl.type == SDL_KEYDOWN )
			{
				switch(eventSdl.key.keysym.sym){
					case SDLK_LEFT: detail+= "izquierda";
									refresh(despX-=10,despY,&rect,1,map,background,screen);
									break;
					case SDLK_RIGHT: detail+= "derecha";
									refresh(despX+=10,despY,&rect,2,map,background,screen);
									 break;
					case SDLK_UP: detail+= "arriba";
								  refresh(despX,despY-=10,&rect,3,map,background,screen);
								  break;
					case SDLK_DOWN: detail+= "abajo";
									refresh(despX,despY+=10,&rect,4,map,background,screen);
								    break;

				}
				logger.reportErrorMessage("Main","main",detail);
				detail.clear();
			}

			//If the user has Xed out the window
			if( eventSdl.type == SDL_QUIT )
			{
				//Quit the program
				quit = true;
			}    
		}
	}
    
	//Free the surfaces
    SDL_FreeSurface( background );
	SDL_FreeSurface( izq );
	SDL_FreeSurface( der );
	SDL_FreeSurface( top );
	SDL_FreeSurface( bot );
    
    //Quit SDL
    SDL_Quit();
    
    //Return
    return 0;    
}